package com.aviatorscript.aviatorscript.example;

import lombok.Data;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 转换系统使用示例
 * 演示如何使用基于AviatorScript的数据转换系统
 */
public class TransformExample {

    /**
     * 源数据类型A - 用户DTO
     */
    @Data
    public static class UserDTO {
        private String userName;
        private Integer age;
        private String email;
        private String phone;
        private String address;
        private String createDate; // 字符串格式的日期
        private Integer status; // 1:正常, 0:禁用
    }

    /**
     * 目标数据类型B - 用户实体
     */
    @Data
    public static class UserEntity {
        private String name;
        private Integer userAge;
        private String emailAddress;
        private String contactPhone;
        private String homeAddress;
        private LocalDateTime createTime;
        private Boolean enabled;
        private String displayName; // 通过脚本生成的显示名称
    }

    /**
     * 转换规则配置示例
     * 
     * 创建转换规则的JSON配置示例：
     * 
     * {
     *   "ruleCode": "USER_DTO_TO_ENTITY",
     *   "ruleName": "用户DTO转实体规则",
     *   "sourceType": "com.aviatorscript.aviatorscript.example.TransformExample$UserDTO",
     *   "targetType": "com.aviatorscript.aviatorscript.example.TransformExample$UserEntity",
     *   "description": "将用户DTO转换为用户实体",
     *   "fieldMappingRules": [
     *     {
     *       "sourceFieldPath": "userName",
     *       "targetFieldPath": "name",
     *       "mappingType": "DIRECT",
     *       "sortOrder": 1,
     *       "enabled": true,
     *       "required": true
     *     },
     *     {
     *       "sourceFieldPath": "age",
     *       "targetFieldPath": "userAge",
     *       "mappingType": "DIRECT",
     *       "sortOrder": 2,
     *       "enabled": true,
     *       "required": false
     *     },
     *     {
     *       "sourceFieldPath": "email",
     *       "targetFieldPath": "emailAddress",
     *       "mappingType": "DIRECT",
     *       "sortOrder": 3,
     *       "enabled": true,
     *       "required": false
     *     },
     *     {
     *       "sourceFieldPath": "phone",
     *       "targetFieldPath": "contactPhone",
     *       "mappingType": "DIRECT",
     *       "sortOrder": 4,
     *       "enabled": true,
     *       "required": false
     *     },
     *     {
     *       "sourceFieldPath": "address",
     *       "targetFieldPath": "homeAddress",
     *       "mappingType": "DIRECT",
     *       "sortOrder": 5,
     *       "enabled": true,
     *       "required": false
     *     },
     *     {
     *       "sourceFieldPath": "createDate",
     *       "targetFieldPath": "createTime",
     *       "mappingType": "SCRIPT",
     *       "transformScript": "source.createDate != nil ? java.time.LocalDateTime.parse(source.createDate + 'T00:00:00') : now",
     *       "sortOrder": 6,
     *       "enabled": true,
     *       "required": false,
     *       "remark": "将字符串日期转换为LocalDateTime"
     *     },
     *     {
     *       "sourceFieldPath": "status",
     *       "targetFieldPath": "enabled",
     *       "mappingType": "SCRIPT",
     *       "transformScript": "source.status == 1",
     *       "sortOrder": 7,
     *       "enabled": true,
     *       "required": false,
     *       "remark": "将状态码转换为布尔值"
     *     },
     *     {
     *       "targetFieldPath": "displayName",
     *       "mappingType": "SCRIPT",
     *       "transformScript": "source.userName + '(' + source.age + '岁)'",
     *       "sortOrder": 8,
     *       "enabled": true,
     *       "required": false,
     *       "remark": "生成显示名称"
     *     }
     *   ]
     * }
     * 
     * 
     * API调用示例：
     * 
     * 1. 创建转换规则：
     * POST /api/transform/rules
     * Content-Type: application/json
     * [上述JSON配置]
     * 
     * 2. 执行数据转换：
     * POST /api/transform/rules/execute/USER_DTO_TO_ENTITY
     * Content-Type: application/json
     * {
     *   "sourceData": {
     *     "userName": "张三",
     *     "age": 25,
     *     "email": "zhangsan@example.com",
     *     "phone": "13812345678",
     *     "address": "北京市朝阳区",
     *     "createDate": "2024-01-15",
     *     "status": 1
     *   },
     *   "targetClass": "com.aviatorscript.aviatorscript.example.TransformExample$UserEntity"
     * }
     * 
     * 3. 刷新规则缓存：
     * POST /api/transform/rules/cache/refresh/USER_DTO_TO_ENTITY
     * 
     * 4. 查询转换规则：
     * GET /api/transform/rules/code/USER_DTO_TO_ENTITY
     */

    /**
     * 复杂脚本转换示例
     * 
     * 以下是一些复杂的AviatorScript脚本示例：
     * 
     * 1. 条件转换：
     * "source.age >= 18 ? '成年人' : '未成年人'"
     * 
     * 2. 字符串处理：
     * "StringUtils.hasText(source.phone) ? source.phone : '未填写'"
     * 
     * 3. 数值计算：
     * "Math.round(source.score * 0.8)"
     * 
     * 4. 日期处理：
     * "source.birthDate != nil ? java.time.Period.between(java.time.LocalDate.parse(source.birthDate), today).getYears() : 0"
     * 
     * 5. 复杂条件：
     * "source.level == 'VIP' && source.points > 1000 ? source.discount * 0.8 : source.discount"
     * 
     * 6. 字符串拼接：
     * "source.province + '-' + source.city + '-' + source.district"
     * 
     * 7. 空值处理：
     * "source.nickname != nil && source.nickname != '' ? source.nickname : source.userName"
     */

    /**
     * 数据库表创建SQL（请先执行这些SQL创建表）
     * 
     * -- 创建数据库
     * CREATE DATABASE IF NOT EXISTS aviator_transform CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
     * USE aviator_transform;
     * 
     * -- 转换规则主表
     * CREATE TABLE `transform_rule` (
     *   `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
     *   `rule_code` varchar(100) NOT NULL COMMENT '规则编码（唯一标识）',
     *   `rule_name` varchar(100) NOT NULL COMMENT '规则名称',
     *   `source_type` varchar(200) NOT NULL COMMENT '源类型完整类名',
     *   `target_type` varchar(200) NOT NULL COMMENT '目标类型完整类名',
     *   `description` varchar(500) DEFAULT NULL COMMENT '规则描述',
     *   `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用（1:启用，0:禁用）',
     *   `compile_status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '编译状态（1:已编译，0:未编译）',
     *   `version` int NOT NULL DEFAULT '1' COMMENT '版本号（用于缓存更新）',
     *   `execute_count` bigint NOT NULL DEFAULT '0' COMMENT '执行次数统计',
     *   `last_execute_time` datetime DEFAULT NULL COMMENT '最后执行时间',
     *   `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
     *   `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
     *   `creator` varchar(50) DEFAULT NULL COMMENT '创建者',
     *   `updater` varchar(50) DEFAULT NULL COMMENT '更新者',
     *   PRIMARY KEY (`id`),
     *   UNIQUE KEY `uk_rule_code` (`rule_code`),
     *   KEY `idx_source_target` (`source_type`, `target_type`),
     *   KEY `idx_enabled_compile` (`enabled`, `compile_status`)
     * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='转换规则主表';
     * 
     * -- 字段映射规则表  
     * CREATE TABLE `field_mapping_rule` (
     *   `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键ID',
     *   `rule_id` bigint NOT NULL COMMENT '转换规则ID',
     *   `source_field_path` varchar(200) NOT NULL COMMENT '源字段路径（支持嵌套，如：user.name）',
     *   `target_field_path` varchar(200) NOT NULL COMMENT '目标字段路径（支持嵌套，如：userInfo.userName）',
     *   `source_field_type` varchar(100) DEFAULT NULL COMMENT '源字段类型',
     *   `target_field_type` varchar(100) DEFAULT NULL COMMENT '目标字段类型',
     *   `mapping_type` varchar(20) NOT NULL DEFAULT 'DIRECT' COMMENT '映射类型（DIRECT:直接映射，SCRIPT:脚本转换，CONSTANT:常量值，FUNCTION:函数转换）',
     *   `transform_script` text COMMENT 'AviatorScript转换脚本',
     *   `constant_value` varchar(1000) DEFAULT NULL COMMENT '常量值（当mapping_type为CONSTANT时使用）',
     *   `default_value` varchar(1000) DEFAULT NULL COMMENT '默认值（当源字段为null时使用）',
     *   `required` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否必填（1:必填，0:非必填）',
     *   `sort_order` int NOT NULL DEFAULT '0' COMMENT '排序顺序',
     *   `enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用',
     *   `remark` varchar(500) DEFAULT NULL COMMENT '备注说明',
     *   `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
     *   `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
     *   PRIMARY KEY (`id`),
     *   KEY `idx_rule_id` (`rule_id`),
     *   KEY `idx_enabled_sort` (`enabled`, `sort_order`),
     *   CONSTRAINT `fk_field_mapping_rule_id` FOREIGN KEY (`rule_id`) REFERENCES `transform_rule` (`id`) ON DELETE CASCADE
     * ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='字段映射规则表';
     */
} 